﻿using AutoMapper;
using FurnitureDesign.Application.Products;
using FurnitureDesign.Application.Products.Dtos;
using FurnitureDesign.Core.Models;
using FurnitureDesign.Core.Repository;
using FurnitureDesign.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FurnitureDesign.Mvc.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class MessageController : BaseController
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IRepository<Message, int> _repository;
        private readonly IMessageService _messageService;
        private readonly IMapper _mapper;
        private readonly IRepository<Message, int> _productRepository;

        public MessageController(ILogger<HomeController> logger, IRepository<Message, int> repository,
          IMessageService messageService, IMapper mapper, IRepository<Message, int> productRepository)
        {
            _logger = logger;
            _repository = repository;
            _messageService = messageService;
            _mapper = mapper;
            _productRepository = productRepository;
        }
        public IActionResult Index()
        {
            return View();
        }
        public async Task<ActionResult> List(MessageInputDto input)
        {
            var stus = await _messageService.GetPagedResult(input);
            return Json(ListResult(_mapper.Map<List<Message>>(stus.Data), stus.TotalCount));
        }
        public async Task<IActionResult> GetEdit(int id)
        {
            Message mess = await _repository.FirstOrDefaultAsync(s => s.Id == id);
            return View(_mapper.Map<Message>(mess));
        }
        public async Task<ActionResult> Edit(Message message)
        {
            message.ReplyTime = Convert.ToString(DateTime.Now);
            message.State = "已回复";
            Message mess = await _repository.UpdateAsync(message);
            if (mess == null)
            {
                return Json(AjaxResult(false));
            }
            return Json(AjaxResult(true));
        }
    }
}
